<?php


function  print_nbsp($count=0){
	$nbsp="";
	for($i=0;$i<$count;$i++){
		$nbsp.="&nbsp;&nbsp;&nbsp;";
	}
	return $nbsp;
}


//检测当前管理登录密码是否正确，否则不允许操作
function check_admin_login_forcurrent($password)
{
    
    $admin_user_id=$_SESSION['admin_id'];
    
    //先得到当前用户里的加密字符
    $ec_salt=M('admin_user')->where(array('user_id'=>$admin_user_id))->getfield('ec_salt');
    
    $where['user_id']=$admin_user_id;
    if($ec_salt){
        $where['password']= md5(md5($password).$ec_salt) ;
    }else{
        $where['password']=  md5($password) ;
    }
    
    $admin_user_entity=M('admin_user')->where($where)->find();
    
    if($admin_user_entity)
    {
        return true;
        
    }
    
    return false;
    

}

/*写文件*/
function pay_logo_aomai_ftp($pay_type,$content){
    $filename =$_SERVER['DOCUMENT_ROOT']. '/jsjp_pay_log/'.$pay_type.date('Y-m-d').'.txt';
    $Ts=fopen($filename,"a+");
    fputs($Ts,"\r\n".date('Y-m-d H:i:s',time()).' - '.$content);
    fclose($Ts);
}

/*交易类型：
1: 收款充值
2: 买货扣除
3: 赠款充值
4: 红冲*/
function getTransactionType($type)
{
	switch ($type) {
		case 1:
			return '收款充值';
			break;
		case 2:
			return '买货扣除';
			break;
		case 3:
			return '赠款充值';
			break;
		case 4:
			return '红冲';
			break;
		case 5:
			return '退款充值';
			break;
		case 6:
			return '收货地址收费';
			break;
	}
}

/*
 * 1：收款充值-来源为ki_purchase_price_list表
2：下单购物-来源为ki_order_info表
3：赠款充值-来源为ki_purchase_price_list表
4：红冲-来源为ki_purchase_price_list表
 */
function getRechargeWay($type){
	switch ($type)
	{
		case 1:
			return '收款充值';
			break;
		case 2:
			return '下单购物';
			break;
		case 3:
			return '赠款充值';
			break;
		case 4:
			return '红冲';
			break;
		case 5:
			return '退款充值';
			break;
	}
}

/*1：现金
2：转账*/
function getReciveMoney($type){
	switch ($type)
	{
		case 1:
			return '现金';
			break;
		case 2:
			return '转账';
			break;
	}
}

//生成以个guid 
function guid() {
	$charid = strtoupper(md5(uniqid(mt_rand(), true)));
	$hyphen = chr(45);// "-"
	$uuid = substr($charid, 0, 8).$hyphen
	.substr($charid, 8, 4).$hyphen
	.substr($charid,12, 4).$hyphen
	.substr($charid,16, 4).$hyphen
	.substr($charid,20,12);
	return $uuid;
}
 

 /**
 * 获得用户的真实IP地址
 *
 * @access  public
 * @return  string
 */
function real_ip()
{
	static $realip = NULL;

	if ($realip !== NULL)
	{
		return $realip;
	}

	if(isset($_COOKIE['real_ipd']) && !empty($_COOKIE['real_ipd'])){
		$realip = $_COOKIE['real_ipd'];
		return $realip;
	}

	if (isset($_SERVER))
	{
		if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
		{
			$arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);

			/* 取X-Forwarded-For中第一个非unknown的有效IP字符串 */
			foreach ($arr AS $ip)
			{
				$ip = trim($ip);

				if ($ip != 'unknown')
				{
					$realip = $ip;

					break;
				}
			}
		}
		elseif (isset($_SERVER['HTTP_CLIENT_IP']))
		{
			$realip = $_SERVER['HTTP_CLIENT_IP'];
		}
		else
		{
			if (isset($_SERVER['REMOTE_ADDR']))
			{
				$realip = $_SERVER['REMOTE_ADDR'];
			}
			else
			{
				$realip = '0.0.0.0';
			}
		}
	}
	else
	{
		if (getenv('HTTP_X_FORWARDED_FOR'))
		{
			$realip = getenv('HTTP_X_FORWARDED_FOR');
		}
		elseif (getenv('HTTP_CLIENT_IP'))
		{
			$realip = getenv('HTTP_CLIENT_IP');
		}
		else
		{
			$realip = getenv('REMOTE_ADDR');
		}
	}

	preg_match("/[\d\.]{7,15}/", $realip, $onlineip);
	$realip = !empty($onlineip[0]) ? $onlineip[0] : '0.0.0.0';

	setcookie("real_ipd", $realip, time()+36000, "/");

	return $realip;
}

//得到属性组大类
function getGoodsAttrCategorys(){
	$goods_attribute_category_list=M('goods_attribute_category')->field('id,name,remark')->order('sort desc')->select();
	return $goods_attribute_category_list;
}

//根据大类ID获取当前类别下的属性分组和属性分组下的属性值
function getGoodsAttributeGroupAndValues($cat_id){
	//如果没有传入类别ID，则返回空对象
	if(!$cat_id){
		return null;
	}

	$goods_attribute_group_list=M('goods_attribute_group')->field('id,name,remark')
	->where(array('cat_id'=>$cat_id,'is_delete'=>0))->order('sort desc')->select();
	//循环属性组，根据属性组ID查询得到属性值
	foreach ($goods_attribute_group_list as $key => $value) {

		$attribute_group_id=$value['attribute_group_id'];

		//查询只查询前台显示的字段，节省内存和加快查询速度
		$goods_attribute_value_list=M('goods_attribute_value')->field('id,value')
		->where(array('attribute_group_id'=>$attribute_group_id,'is_delete'=>0))->order('sort desc')->select();

		$goods_attribute_group_list[$key]['_child']=$goods_attribute_value_list;
	}

	return $goods_attribute_group_list;
}

/* 获取商品分类 */
function getGoodsCategory($cat_id){
	$cat_id = intval($cat_id);
	$cat_name = M('category')->where(array('cat_id'=>$cat_id))->getField('cat_name');
	return empty($cat_name)?'-':$cat_name;
}

function getGoodsBrandName($brand_id){
	$brand_id = intval($brand_id);
	$brand_name = M('brand')->where(array('brand_id'=>$brand_id))->getField('brand_name');
	return empty($brand_name)?'-':$brand_name;
}

function getCurrentPriceTypeName($typeid){
	$arr[1]='零售价';
	$arr[2]='会员价';
	$arr[3]='VIP价';
	return $arr[$typeid];
}


    function http_request($url, $data = null){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }
?>

